#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"processing.h"
#include "image_processing.h"
#include"qt_cv_change.h"
#include"setting_size.h"
#include"change_bk.h"
#include <opencv2/dnn.hpp>
#include <opencv2/opencv.hpp>
#include <QDebug>
#include<stdio.h>
#include<iostream>
using namespace cv::dnn;
using namespace std;

//col 宽度
//row 高度
using namespace cv;

//floor()是向下取整函数
int MainWindow::doubleToInt(double d)
{
    double intPart = floor(d);
    if ((d - intPart) >= (double)0.5)
    {
        return (intPart + 1);
    }
    else
    {
        return intPart;
    }
}
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->menuBar->setNativeMenuBar(false);
    ui->label->setScaledContents(true);
    ui->label->resize(ui->widget->size());
    ui->stackedWidget->setCurrentIndex(3);
}

MainWindow::~MainWindow()
{
    delete ui;
}

//void Widget::displayMat(const cv::Mat &image)
void MainWindow::show_onlabel(){
    QImage* img=new QImage;
    *img= cvMat2QImage(dstImage);
    *img = img->convertToFormat(QImage::Format_RGB32);
    if(dstImage.cols<640){
        if(dstImage.rows<360){
            ui->widget->resize(dstImage.cols,dstImage.rows);
        }
        else{
            double m=dstImage.rows;
            m=360/m;
            //int q=doubleToInt(m);
            int n=doubleToInt(dstImage.cols*360/dstImage.rows);
            if(!n){
                qDebug()<<"no";
            }
            ui->widget->resize(n,360);
        }
    }
    else{
        if(dstImage.rows>360){
            double m=dstImage.rows;
            m=360/m;
            //int q=doubleToInt(m);
            int n=doubleToInt(dstImage.cols*360/dstImage.rows);
            if(!n){
                qDebug()<<"no";
            }
            ui->widget->resize(n,360);
        }
        else{
            double m=dstImage.cols;
            m=640/m;
            int n=doubleToInt(dstImage.rows*(640/dstImage.cols));
            ui->widget->resize(640,n);
        }
    }
    ui->label->setScaledContents(true);
    ui->label->resize(ui->widget->size());
    ui->label->setPixmap(QPixmap::fromImage(*img));
    delete img;
}

void MainWindow::on_action_Open_triggered()
{
    QString curDir=QDir::currentPath();//选择文件
    QString filename=QFileDialog::getOpenFileName(this,"select Image",curDir,
                                                          "Images (*.png *.bmp *.jpg *.tif *.GIF )");
    if(filename.isEmpty())
    {
        qDebug()<<"图像失败";
         return;
    }
    QImage* img=new QImage;
    if(! ( img->load(filename) ) ) //加载图像
    {
             qDebug()<<"打开图像失败";
             //ui->debugEdit->append("打开图像失败");
         delete img;
         return;
    }

    *img = img->convertToFormat(QImage::Format_RGB32);

    srcImage = QImage2cvMat(*img);
    cvtColor(srcImage,dstImage,COLOR_RGBA2RGB);
    cvtColor(srcImage,srcImage,COLOR_RGBA2RGB);
    if(dstImage.type()==CV_8UC4){
        qDebug()<<"还是不行";
    }
    //ui->label->resize(ui->widget->size());
    //ui->label->setPixmap(QPixmap::fromImage(*img));
    //ui->label->setPixmap(QPixmap::fromImage(*img).scaled(ui->label->size()));
    //dstImage=srcImage;
    show_onlabel();
    delete img;
}

void MainWindow::on_action_Save_triggered()
{
    QString curDir=QDir::currentPath();//选择文件
    QString filename=QFileDialog::getSaveFileName(this,"save Image",curDir,
                                                      "Images (*.png *.bmp *.jpg *.tif *.GIF )");
    if(filename.isEmpty()||dstImage.empty())
    {
         return;
    }
    std::string save_path = filename.toLocal8Bit().toStdString();
    //save_path+=".jpg";//std::vector<int> compression_params;
    //compression_params.push_back(IMWRITE_JPEG_QUALITY);
    //compression_params.push_back(100);
    imwrite(save_path,dstImage);//保存图片
}

void MainWindow::on_actionJPG_triggered()
{
    QString curDir=QDir::currentPath();//选择文件
    QString filename=QFileDialog::getSaveFileName(this,"save Image",curDir,
                                                      "Images (*.png *.bmp *.jpg *.tif *.GIF )");
    if(filename.isEmpty()||dstImage.empty())
    {
         return;
    }
    std::string save_path = filename.toLocal8Bit().toStdString();
    //save_path+=".jpg";//std::vector<int> compression_params;
    //compression_params.push_back(IMWRITE_JPEG_QUALITY);
    //compression_params.push_back(100);
    size_t n=save_path.find('.');
    save_path.resize(n, ' ');
    save_path+=".jpg";
    imwrite(save_path,dstImage);//保存图片
}

void MainWindow::on_actionPNG_triggered()
{
    QString curDir=QDir::currentPath();//选择文件
    QString filename=QFileDialog::getSaveFileName(this,"save Image",curDir,
                                                      "Images (*.png *.bmp *.jpg *.tif *.GIF )");
    if(filename.isEmpty()||dstImage.empty())
    {
         return;
    }
    std::string save_path = filename.toLocal8Bit().toStdString();
    //save_path+=".jpg";//std::vector<int> compression_params;
    //compression_params.push_back(IMWRITE_JPEG_QUALITY);
    //compression_params.push_back(100);
    size_t n=save_path.find('.');
    save_path.resize(n, ' ');
    save_path+=".png";
    imwrite(save_path,dstImage);//保存图片
}

void MainWindow::on_actionTIFF_triggered()
{
    QString curDir=QDir::currentPath();//选择文件
    QString filename=QFileDialog::getSaveFileName(this,"save Image",curDir,
                                                      "Images (*.png *.bmp *.jpg *.tif *.GIF )");
    if(filename.isEmpty()||dstImage.empty())
    {
         return;
    }
    std::string save_path = filename.toLocal8Bit().toStdString();
    //save_path+=".jpg";//std::vector<int> compression_params;
    //compression_params.push_back(IMWRITE_JPEG_QUALITY);
    //compression_params.push_back(100);
    size_t n=save_path.find('.');
    save_path.resize(n, ' ');
    save_path+=".tiff";
    imwrite(save_path,dstImage);//保存图片
}

void MainWindow::on_pushButton_cgsz_clicked()
{
    int cols=dstImage.cols,rows=dstImage.rows;
    setting_size* dlgtablesize=new setting_size(this);
    int ret=dlgtablesize->exec();
    if(ret==QDialog::Accepted){
        cols=dlgtablesize->columncount();
        rows=dlgtablesize->rowcount();
    }
    delete dlgtablesize;
    cv::resize(dstImage,dstImage,Size(cols,rows),0,0,INTER_AREA);
    //srcImage=dstImage;
    show_onlabel();
}

void MainWindow::on_pushButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}

void MainWindow::on_pushButton_2_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::on_pushButton_3_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
}

void MainWindow::on_pushButton_phfix_clicked()
{
    Mat img_grey,kernel;
    //qDebug()<<"我走了";
    cvtColor(dstImage,img_grey,COLOR_RGB2GRAY,0);
    //qDebug()<<"我走了";
    threshold(img_grey,maskImage,245,255,THRESH_BINARY);
    kernel=getStructuringElement(MORPH_RECT,Size(10,10));
    dilate(maskImage,maskImage,kernel);
    inpaint(dstImage,maskImage,dstImage,30,INPAINT_TELEA);
    show_onlabel();
}

void MainWindow::on_pushButton_grey_clicked()
{
    imageprocess* image = new imageprocess();
    dstImage = image->grey_all();
    show_onlabel();
}

void MainWindow::on_pushButton_renew_clicked()
{
    dstImage=srcImage;
    show_onlabel();
}

void MainWindow::on_pushButton_cgbk_clicked()
{
    change_bk* change=new change_bk();
    int ret=change->exec();
    if(ret==QDialog::Accepted){
        imageprocess* image = new imageprocess();
        dstImage = image->change_phbg();
        //qDebug()<<"没有";
        //qDebug()<<maskbk.type();
        //qDebug()<<maskImage.type();
        //qDebug()<<dstImage.type();
        if(maskbk.rows!=0){
            Mat temp=maskbk-maskImage;
            dstImage=temp+dstImage;
        }
       //+dstImage;
        //qDebug()<<"没有";
        //addWeighted(maskbk,0.5,maskImage,0.5,0.0,dstImage,-1);
        //dstImage = maskbk-maskImage;//+dstImage;
    }
    show_onlabel();
}

void MainWindow::on_pushButton_rgfc_clicked()
{
    imageprocess* image=new imageprocess();
    dstImage=image->rgfc();
    show_onlabel();
}

void MainWindow::on_pushButton_soft_clicked()
{
    imageprocess* image = new imageprocess();
    dstImage = image->soft_all();
    show_onlabel();
}

void MainWindow::on_pushButton_hard_clicked()
{
    imageprocess* image = new imageprocess();
    dstImage = image->hard_all();
    show_onlabel();
}

void MainWindow::on_pushButton_ai_clicked()
{
    imageprocess* image = new imageprocess();
    dstImage = image->ai();
    show_onlabel();
}

void MainWindow::on_pushButton_4_clicked()
{
    imageprocess* image = new imageprocess();
    dstImage = image->two_cut();
    show_onlabel();
}

void MainWindow::on_pushButton_clean_clicked()
{
    on_pushButton_phfix_clicked();
}
